package visualcbir;

import iautils.IAUtils;
import imganalysis.Cluster;
import imganalysis.DataUnit;
import imganalysis.KMeans;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import visualcbir.util.VSUtils;
import visualcbir.node.ModelNode;

/**
 *
 * @author sheila
 */

public class ModelComposedGroups  extends ModelComposed
{
    public ModelComposedGroups(){
        super();
    }

    /*
     * To visualize a fixed image set of clusters for a two levels visual structure.
     * Path_folder/ (String path)
     *   |
     *   Center.jpeg
     *   Cluster1/
     *   Cluster2/
     *   Cluster3/
     *      |
     *      Center.jpeg
     *      Object1.jpeg
     *      Object2.jpeg
     *      Object3.jpeg
     *      ...
     *
     */     
    public void getFixedData(String path, int numClusters, int numImgsCluster){

         this.addModel(new ModelNode(0, path + "Center.jpeg", "name", 0, 1.0));
         int numClustersFiles = VSUtils.countFilesByRegex(path, "Cluster[0-9]+");
         numClusters = (numClusters < numClustersFiles)?numClusters:numClustersFiles;
         
         for (int iCurrentCluster = 1; (iCurrentCluster <=  numClusters); iCurrentCluster++) {
            String clusterpath = "Cluster"+iCurrentCluster+"/";
            ModelComposed oCurrentModelComposed = new ModelComposed();            
            oCurrentModelComposed.addModel(new ModelNode(iCurrentCluster, path + clusterpath + "Center.jpeg", "name", iCurrentCluster, 1.0));
            int numImgsCurrCluster = VSUtils.countFilesByRegex(path + clusterpath, "Object[0-9]+\\.jpeg");
            numImgsCurrCluster = (numImgsCluster < numImgsCurrCluster)?numImgsCluster:numImgsCurrCluster;            

            for (int iCurrentImage = 1; iCurrentImage <= numImgsCurrCluster; iCurrentImage++) 
                oCurrentModelComposed.addModel(new ModelNode(iCurrentImage, path + clusterpath + "Object" + String.valueOf(iCurrentImage) + ".jpeg" , "name", iCurrentImage, 1.0));             
            this.addModel(oCurrentModelComposed);
        }
    }

    public void getData( int idQueryImage, int numClusters, int numVisibleClusters, ArrayList<DataUnit> aDataUnit ){
        KMeans oKMeans;
        if(aDataUnit==null){
            String path = VSUtils.getProperty("imageshistograms");
            ArrayList <int[]> aiHistograms = IAUtils.readHistograms(path);
            oKMeans = new KMeans(aiHistograms, numClusters, idQueryImage);//Clusterize all Feature Vectors
        }
        else
            oKMeans = new KMeans(numClusters,aDataUnit, idQueryImage);


        oKMeans.orderingClustersByCentroid(idQueryImage);
        for (int i = 0; i <numVisibleClusters; i++)    oKMeans.orderingDataUnits(i);// aClusters.size()

         ArrayList<Cluster> aClusters = oKMeans.getAClusters();

        //get query image //duas linhas adicionadas
        String imgPath = IAUtils.readLineFromImagesFile(idQueryImage);
        this.addModel(new ModelNode(idQueryImage, imgPath, "name", idQueryImage, 1.0));
        ///this.oNodeModelQuery = new ModelNode(imgPath, "name", queryImage, 1.0);
        // get Images to visualize

        for (int iCurrentCluster = 0; iCurrentCluster <  numClusters ; iCurrentCluster++) {//TROCARRR // aClusters.size()
            Cluster oCurrentCluster = aClusters.get(iCurrentCluster);//ImgAnalysis
            ModelComposed oCurrentModelComposed = new ModelComposed();
            int numImagesPerCluster=28;
            if(oCurrentCluster.getNumDataUnits()<numImagesPerCluster) numImagesPerCluster = oCurrentCluster.getNumDataUnits();

            for (int iCurrentDU = 0; iCurrentDU < numImagesPerCluster; iCurrentDU++) {
                int idImg=oCurrentCluster.getDataUnit(iCurrentDU).getId();
                imgPath = IAUtils.readLineFromImagesFile(idImg);
                oCurrentModelComposed.addModel(new ModelNode(idImg, imgPath, "name", iCurrentDU, 1.0));
            }
            this.addModel(oCurrentModelComposed);
        }
    }

     public void getDataFractal( int idQueryImage, int numClusters, int numVisibleClusters, ArrayList<DataUnit> aDataUnit ){
        KMeans oKMeans;
        if(aDataUnit==null){
            String path = VSUtils.getProperty("imageshistograms");
            ArrayList <int[]> aiHistograms = IAUtils.readHistograms(path);
            oKMeans = new KMeans(aiHistograms, numClusters, idQueryImage);//Clusterize all Feature Vectors
        }
        else
            oKMeans = new KMeans(numClusters,aDataUnit, idQueryImage);


        oKMeans.orderingClustersByCentroid(idQueryImage);
        for (int i = 0; i <numVisibleClusters; i++)    oKMeans.orderingDataUnits(i);// aClusters.size()

         ArrayList<Cluster> aClusters = oKMeans.getAClusters();

        //get query image //duas linhas adicionadas
        String imgPath = IAUtils.readLineFromImagesFile(idQueryImage);
        this.addModel(new ModelNode(idQueryImage, imgPath, "name", idQueryImage, 1.0));
        ///this.oNodeModelQuery = new ModelNode(imgPath, "name", queryImage, 1.0);
        // get Images to visualize

        for (int iCurrentCluster = 0; iCurrentCluster <  numClusters ; iCurrentCluster++) {//TROCARRR // aClusters.size()
            Cluster oCurrentCluster = aClusters.get(iCurrentCluster);//ImgAnalysis
            ModelComposed oCurrentModelComposed = new ModelComposed();
            int numImagesPerCluster=28;
            if(oCurrentCluster.getNumDataUnits()<numImagesPerCluster) numImagesPerCluster = oCurrentCluster.getNumDataUnits();
                
               for (int iCurrentCluster2 = 0; iCurrentCluster2 <  numClusters ; iCurrentCluster2++) {//TROCARRR // aClusters.size()
                Cluster oCurrentCluster2 = aClusters.get(iCurrentCluster2);//ImgAnalysis
                ModelComposed oCurrentModelComposed2 = new ModelComposed();
                int numImagesPerCluster2=10;
                if(oCurrentCluster2.getNumDataUnits()<numImagesPerCluster2) numImagesPerCluster2 = oCurrentCluster2.getNumDataUnits();

                    for (int iCurrentDU = 0; iCurrentDU < numImagesPerCluster2; iCurrentDU++) {
                        int idImg=oCurrentCluster2.getDataUnit(iCurrentDU).getId();
                        imgPath = IAUtils.readLineFromImagesFile(idImg);
                        oCurrentModelComposed2.addModel(new ModelNode(idImg, imgPath, "name", iCurrentDU, 1.0));
                    }

                this.addModel(oCurrentModelComposed2);

            this.addModel(oCurrentModelComposed);
        }}
    }
     public void getData(int numImages){
        //******************* TEMPORAL *****************************
        try{
            String path = VSUtils.getProperty("imagesnames");
            FileInputStream fstream = new FileInputStream(path);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            for(int i=0; i<10 ; i++) {
                ModelComposed ocurr=new ModelComposed();
                this.addModel(ocurr);
                for(int j=0; j<10 ; j++){
                    ModelComposed ocurr2=new ModelComposed();
                    ocurr.addModel(ocurr2);
                    for(int k=0; k<10 ; k++)
                        ocurr2.addModel(new ModelNode(k,"aaa", "name", k, 1.0));
                }
            }
            in.close();
        }catch (Exception e){   System.err.println("Error getting the data: " + e.getMessage());    }
    }

}
